package com.dqhl.community.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.dqhl.community.R;
import com.dqhl.community.adapter.FamilyServiceRecordAdapter;
import com.dqhl.community.base.BaseActivity;
import com.dqhl.community.model.FamilyServiceRecordBean;
import com.dqhl.community.util.Config;
import com.dqhl.community.util.Dlog;
import com.dqhl.community.util.JsonUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

/**
 * Created by LIENLIN on 2016/9/11.
 * 家政服务预约记录
 */
public class FamilyServiceRecordActivity extends BaseActivity implements View.OnClickListener {

    private ImageView ivTopBack;
    private TextView tvTopTitle;
    private ListView lvRecord;
    private TextView tvNoInfo;
    private List<FamilyServiceRecordBean> familyServiceList;
    private FamilyServiceRecordAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_appliance_repair_record);
        initView();
        getData();
    }
    //  获取数据
    private void getData() {
        showCustomLoadBar(R.string.loading);
        RequestParams params = new RequestParams(Config.Api.family_order);
        params.addBodyParameter("userid",app.getUserId());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                int errCode = JsonUtils.getErrCode(result);
                Dlog.e("onSuccess"+result);
                String data = JsonUtils.getInfo(result,"data");
                if (errCode == 0) {
                    /*familyServiceList = new ArrayList<FamilyServiceRecordBean>(
                            JSONArray.parseArray(data, FamilyServiceRecordBean.class)
                    );*/
                    familyServiceList = JSON.parseArray(data, FamilyServiceRecordBean.class);
                    Dlog.e("onSuccess" + familyServiceList.toString());
                    if (familyServiceList.size() <= 0) {
                        tvNoInfo.setVisibility(View.VISIBLE);
                        lvRecord.setVisibility(View.GONE);
                    } else {
                        adapter =new FamilyServiceRecordAdapter(
                                FamilyServiceRecordActivity.this, familyServiceList, new FamilyServiceRecordAdapter.onCompleteListener() {
                            @Override
                            public void onComplete(int position) {
                                doComplete(familyServiceList.get(position).getH_id());
                            }
                        });
                        lvRecord.setAdapter(adapter);
                    }
                }
                hideCustomLoadBar();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
        hideCustomLoadBar();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void initView() {
        ivTopBack = (ImageView) findViewById(R.id.iv_top_back);
        ivTopBack.setVisibility(View.VISIBLE);
        tvTopTitle = (TextView) findViewById(R.id.tv_top_title);
        tvTopTitle.setText(R.string.tv_family_record);
        lvRecord = (ListView) findViewById(R.id.lv_record);
        tvNoInfo = (TextView) findViewById(R.id.tv_no_info);

        ivTopBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_top_back:
                finish();
                break;
        }

    }
    //完成
    private void doComplete(String position) {
        showCustomLoadBar(".");
        RequestParams parmas = new RequestParams(Config.Api.housekeepingFinish);
        parmas.addBodyParameter("s_id", position);
        x.http().post(parmas, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Dlog.e("GGonSuccess" + result);
                int errCode = JsonUtils.getErrCode(result);
                String errMsg = JsonUtils.getErrMsg(result);
                if (errCode == 0) {
                    toast(errMsg);
                    getData();
                } else {
                    toast(errMsg);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                hideCustomLoadBar();
            }
        });
    }


}
